package world.pieces;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;

import javax.imageio.ImageIO;

import utility.Color;
import utility.Path;
import utility.Point;
import world.Piece;


public class Pown extends Piece implements Cloneable, Serializable{

	private boolean debug=false;
	public Pown(Color c) {
		this.color=c;
	}
	public boolean canMove(Point from,Point to) {
		if(this.color==Color.BLACK){
			if(from.getX()==1){
				if(to.getX()-from.getX()==1 && to.getY()==from.getY()){
					return true;
				}
				if(to.getX()-from.getX()==2 && to.getY()==from.getY()){
					return true;
				}
				if(to.getX()-from.getX()==1 && to.getY()==from.getY()-1){
					return true;
				}
				if(to.getX()-from.getX()==1 && to.getY()==from.getY()+1){
					return true;
				}
			}
			else{
				if(to.getX()-from.getX()==1 && to.getY()==from.getY()){
					return true;
				}
				if(from.getX()-to.getX()==-1 && to.getY()==from.getY()+1){
					return true;
				}
				if(from.getX()-to.getX()==-1 && to.getY()==from.getY()-1){
					return true;
				}
				else
					if(debug){
						System.out.println("Pown can't move from " +from +" to "+to);
					}
					return false;
			}
		}
		if(this.color==Color.WHITE){
			if(from.getX()==6){
				if(from.getX()-to.getX()==1 && to.getY()==from.getY()){
					return true;
				}
				if(from.getX()-to.getX()==2 && to.getY()==from.getY()){
					return true;
				}
				if(to.getX()-from.getX()==-1 && to.getY()==from.getY()-1){
					return true;
				}
				if(to.getX()-from.getX()==-1 && to.getY()==from.getY()+1){
					return true;
				}
			}
			else{
				if(from.getX()-to.getX()==1 && to.getY()==from.getY()){
					return true;
				}
				if(from.getX()-to.getX()==1 && to.getY()==from.getY()+1){
					return true;
				}
				if(from.getX()-to.getX()==1 && to.getY()==from.getY()-1){
					return true;
				}
				else
					if(debug){
						System.out.println("Pown can't move from " +from +" to "+to);
					}
					return false;
			}
		}
		if(debug){
			System.out.println("Pown can't move from " +from +" to "+to);
		}
		return false;
	}
	@Override
	public String toString() {
		// TODO Auto-generated method stub
		return "(Pown "+color.getColor()+")"+position;
	}
	public BufferedImage get2dImage() {
		// TODO Auto-generated method stub
		if(this.color.equals(Color.WHITE)){
			try {
				return  ImageIO.read(new File(Path.getPathPiecesWhite()+"pown-white.png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(this.color.equals(Color.BLACK)){
			try {
				return  ImageIO.read(new File(Path.getPathPiecesBlack()+"pown-black.png"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}
	@Override
	public Object clone() {
		// TODO Auto-generated method stub
		return super.clone();
	}
	@Override
	public String get2DPath() {
		if(this.color.equals(Color.WHITE)){

				return  Path.getPathPiecesWhite()+"pown-white.png";
	
		}
		else if(this.color.equals(Color.BLACK)){
			
				return  Path.getPathPiecesBlack()+"pown-black.png";

		}else{
			return "";
		}
	}
}
